*Set default directory
cd "C:\Users\scott\Documents\publication\Xi Jinping Effect"

*start new log file
cap log close
log using Xi_Effect.log, text replace

*Final Project
*24 December 2020
*Scott D. McDonald

clear

*import dataset
use S002 S003 S007 A004 A008 A025 A168 A168A D059 E010 E012 E017 E018 E023 E026 E026B E027 E032 E035 E036 E037 E069_02 E069_04 E069_06 E069_11 E069_12 E069_13 E069_17 E069_20 E069_33 E069_60 E107 E110 E111 E114 E115 E121 E122 E123 E128 G006 X001 X002 X007 X023 X025CSWVS X028 X047R using "C:\Users\scott\Documents\FLETCHER\2020 Fall\EIB-213\Final Project\WVS_TimeSeries_stata_v1_5.dta", clear

*format dataset

*only the People's Republic of China
keep if S003==156

*set wave dummies
gen wave2=(S002==2)
gen wave3=(S002==3)
gen wave4=(S002==4)
gen wave5=(S002==5)
gen wave6=(S002==6)
gen wave7=(S002==7)
gen dream=(S002==6 | S002==7)


*remove non response of key variables
drop if E023 <=0
drop if E069_12 <=0

*confidence in ccp with rising values
gen conf_ccp=.
replace conf_ccp=0 if E069_12==4 & E069_12!=.
replace conf_ccp=1 if E069_12==3 & E069_12!=.
replace conf_ccp=2 if E069_12==2 & E069_12!=.
replace conf_ccp=3 if E069_12==1 & E069_12!=.

*political interest dummy
gen polint=.
replace polint=0 if E023==3 | E023==4 & E023!=.
replace polint=1 if E023==1 | E023==2 & E023!=.

*interaction variable
gen xi_effect=(dream*polint)

*confidence in government dummy
gen conf_gov=.
replace conf_gov=0 if E069_11==3 | E069_11==4 & E069_11!=.
replace conf_gov=1 if E069_11==1 | E069_11==2 & E069_11!=.

*confidence in media dummy
gen conf_press=.
replace conf_press=0 if E069_04==3 | E069_04==4 & E069_04!=.
replace conf_press=1 if E069_04==1 | E069_04==2 & E069_04!=.

*government v personal responsibility dummy
gen gov_res=.
replace gov_res=0 if E037==1 | E037==2 | E037==3 | E037==4 | E037==5 & E037!=.
replace gov_res=1 if E037>=6 & E037!=.

*nationalism dummy
gen nationalism=.
replace nationalism=0 if G006==3 | G006==4 & G006!=.
replace nationalism=1 if G006==1 | G006==2 & G006!=.

*respect for authority dummy
gen res_authority=.
replace res_authority=0 if E018==3 & E018!=.
replace res_authority=1 if E018==1 | E018==2 & E018!=.

*confidence in military dummy
gen conf_mil=.
replace conf_mil=0 if E069_02==3 & E069_02!=.
replace conf_mil=1 if E069_02==1 | E069_02==2 & E069_02!=.

*Is politics important in life dummy
gen import_pol=.
replace import_pol=0 if A004==3 | A004==4 & A004!=.
replace import_pol=1 if A004==1 | A004==2 & A004!=.

*demographic dummoies
gen female=(X001==2)
gen married=(X007==1)
gen year=.
replace year=1990 if S002==2 & S002!=.
replace year=1995 if S002==3 & S002!=.
replace year=2001 if S002==4 & S002!=.
replace year=2007 if S002==5 & S002!=.
replace year=2013 if S002==6 & S002!=.
replace year=2018 if S002==7 & S002!=.
gen age=(year-X002)

*years of education dummy
gen degree=.
replace degree=1 if X023>=21 & X023!=.
replace degree=0 if X023<=20 & X023!=.

****Run the Regressions*****

*basic regression (Model 1)
ologit conf_ccp polint
estimates store model1

*basic dif-in-dif regression (model 2)
ologit conf_ccp xi_effect dream polint
estimates store model2

*dif-in-dif with controls (model 3)
ologit conf_ccp xi_effect dream polint age female married X047R import_pol
estimates store model3

*dif-in-dif with controls (model 4)

ologit conf_ccp xi_effect dream polint age female married X047R import_pol if S002>=5 &S002<=6
estimates store model4

*dif-in-dif with additionaly confidence controls (model 5)
ologit conf_ccp xi_effect dream polint conf_gov conf_mil conf_press gov_res nationalism res_authority
estimates store model5

*dif-in-dif with best of all controls (model 6)
ologit conf_ccp xi_effect dream polint age female married X047R import_pol conf_press gov_res res_authority
estimates store model6

*table creation
estimates table model1 model2 model3 model4 model5 model6, b(%10.3f) se(%10.3f) stats(rmse r2 N) title(Xi Jinping Effect on Confidence in the CCP)

log close

reg error stop run

gen polunint=.
replace polunint=1 if E023==3 | E023==4 & E023!=.
replace polunint=0 if E023==1 | E023==2 & E023!=.

gen xi_massline=(dream*polunint)


*other things I tried
reg conf_ccp polint dream xi_effect if S002>=5 &S002<=6, r
*led to statistically insignificant dream 10% and xi_effect 5%n (but CI including zero). Coefficients changes slightly for xi_effect, but over 3 SE for dream.

*dif-in-dif with PEC (model3)
reg conf_ccp polint dream xi_effect PEC, r
estimates store model3

*dif-in-dif with age (model4)
reg conf_ccp polint dream xi_effect age, r
estimates store model4

*Individuals subject to Patriotic Education Campaign dummy
gen PEC=(X002>=1978)

reg conf_ccp dream, r

gen scidev=(S002>=5)

reg conf_ccp polint dream xi_effect female E069_02 workst import_pol, r

reg conf_ccp_bi polint dream xi_effect, r

reg nationalism polint dream xi_effect X002 female married X047R import_pol, r

*dif-in-dif
reg conf_gov polint wave6 xi_effect, r
reg conf_gov polint wave5 hu_effect, r
reg conf_gov polint wave2 jiang_effect, r

reg nationalism polint wave6 xi_effect, r
reg nationalism polint wave5 hu_effect, r
reg nationalism polint wave2 jiang_effect, r

reg conf_ccp polint wave6 xi_effect, r
reg conf_ccp polint wave5 hu_effect, r
reg conf_ccp polint wave2 jiang_effect, r

reg conf_press polint wave6 xi_effect, r
reg conf_press polint wave5 hu_effect, r
reg conf_press polint wave2 jiang_effect, r


*Fun with graphs. Saving for future reference and use
graph twoway (scatter conf_ccp polint) (lfit conf_ccp polint)
graph twoway (scatter conf_ccp polint) (lfit conf_ccp polint) (lfit conf_ccp dream) (lfit conf_ccp xi_effect) if S002>=5 &S002<=6
*graph twoway (scatter visitno age) (lfit visitno age)
*graph twoway (scatter visitno age, color(gs6) msymbol(Oh) msize(vtiny) jitter (6))

*clean data 36=Australia, 156=PRC, 158=taiwan, HK=334 392=japan, 410=ROK, 702=singapore, 840=USA
*keep if S003==36 | S003==156 | S003==158 | S003==344 | S003==392 | S003==410 | S003==702 | S003==840

*working status dummy
gen workst=.
replace workst=1 if X028==1 | X028==2 & X028!=.
replace workst=0 if X028==.

*set country dummies
gen Aus=(S003==36)
gen prc=(S003==156)
gen taiwan=(S003==158)
gen hk=(S003==344)
gen japan=(S003==392)
gen rok=(S003==410)
gen singapore=(S003==702)
gen usa=(S003==840)

Some W with some effect
A004
X007
res_authority


*dif-in-dif fo hu with controls (model 8)
reg conf_ccp polint scidev hu_effect X002 female married X047R import_pol , r
estimates store model8

Possible investigations
E018 Greater respect for authority is good
E069_04 Confidence in press
E069_06 Confidence in Police
E069_11 Confidence in government
E069_12 Confidence in political parties
E069_17 Justic system
E037 Government responsibility 1-10 scale turn into dummy***
E114 Political systesm have strong leaders
E115 Having experts make decisions
G006 Pride in this nationality.


Cross-setion Dif in Dif-- Y X G D W W W
X treatment = G*D
G binary variable indicating whether individual is in the treatment group
D is the binary that equals 0 in the first period and 1 in the second

*use S002 S003 S007 A004 A008 A025 A168 A168A D059 E010 E012 E017 E018 E023 E026 E026B E027 E032 E035 E036 E037 E069_02 E069_04 E069_06 E069_11 E069_12 E069_13 E069_17 E069_20 E069_33 E069_60 E107 E110 E111 E114 E115 E121 E122 E123 E128 E196 G006 X001 X002 X007 X023 X025CSWVS X028 X047R Y011A Y011B using "C:\Users\scott\Dropbox\FLETCHER\2020--Fall\EIB-213\Final Project\WVS_Longitudinal_1981_2016_stata_v20180912.dta", clear

*Includes wave 7, but no nationalism or authority variable
*use A004 S002 S003 S007 A008 A025 A062 A068 A168 A168A D059 E007 E008 E009 E010 E012 E017 E018 E023 E026 E026B E027 E032 E034 E035 E036 E037 E046 E057 E058 E059 E060 E061 E063 E066 E069_02 E069_04 E069_06 E069_11 E069_12 E069_13 E069_17 E069_20 E069_29 E069_33 E069_38 E069_54 E069_60 E069_61 E107 E110 E111 E112 E113 E114 E115 E120 E121 E122 E123 E125 E127 E128 E248 E268 E269 E270 E271 E272 E273 E274 X001 X002 X007 X023 X025CSWVS X028 X047R Y011A Y011B using "C:\Users\scott\Dropbox\FLETCHER\2020--Fall\EIB-213\Final Project\WVS_TimeSeries_stata_v1_5.dta", clear

*gen d_midat=.
*replace d_midat=1 if region==2 & region!=.
*replace d_midat=0 if region==4 | region==8 & region!=.

*regressed nationalism on PRC, got small positive coeeficient. Then regressed same adding a time fixed effect and the coefficient changed (over 4 SE) and R2 went up from 0.5% to 3.3%. This suggests the year is correlated with nationalism
reg Y011B prc, r
reg Y011B prc i.S002, r
*regressed inverst respect for authority (defiance) on prc. small, but negative (meaning respect for authority) on PRC. added time fixed effect and changed significantly (over 6 SE), suggesting the time effects are correlated.
reg Y011A prc, r
reg Y011A prc i.S002, r
*RD analysis
reg Y011B prc wave6 xi_effect, r
reg Y011B wave6 if prc==1, r

reg trust_bi Dt if d_midat==1, r
*Background Concepts

*Let hy6(t) be the 3-month holding yield (in %) from buying a 6-month Treasury-bill at time (t-1) and selling it at time t (after 3 months) when it will be equivalent to a 3-month Treasury-bill.  Let hy3(t-1) be the 3-month holding yield from buying a 3-month Treasury-bill and time (t-1).  The expectations hypothesis (EH) of finance says that these two different 3-month investments should yield the same return (on average).  We can represent the relationship between the two yields as hy6(t) = β0 + β1 hy3(t-1) + u(t) and the EH as H0 : β0 = 0 &  β1 = 1.

Instrumental need the 2sls because stata does a few 
ivregress 2sls Y (X = Z) W, r first
can use option "first" to get an output of the first stateg "variabls.., r first"

Test instruments
runn ivregress with Z>X
predict uhat_overid, resid
reg uhat_overid Z1 Z2 W W
test Z1 Z2
If Zs are valid instruments they will not be correlated with uhat. can check with the p values on this regression. Test is just confirmation. Must not be correlated with uhat to be a valid instruments

log close

*error correction model*
*reg D1.hy6 D1.hy3_1 L1.uhat

*useful commands
*twoway (tsline cpi, yaxis (1)) (tsline m2 , yaxis (2))
*twoway (tsline lcpi , yaxis (1)) (tsline lm2 , yaxis(2))

*f test an exact slope and intercept
*test (variable = .6) (_cons = -1) Tests an exact 

*save the graph as a pdf
*graph export TS_Graph1.pdf, as (pdf) replace

*check for deterministic trend, regress the varaible on the time index
*reg lcpi tindex

*check for stochastic trend aka unit roots
*reg D1.variable L1.variable tindex L1.D.1variable L2.D1. variables

*check to see if I(1) or I(2) -- means check D-F on first diiference 
*reg D2.variable L1.D1.variable tindex L1.D2.variable L2.D2.variables

*cointegration testing
*reg variabl variables
*if the above has a large R2 and high t-stat suggest relationship. But unless they are cointegrationed you cannot trust this regression and you cannot trust this t-stat. cointegration necessary before I can trust, so predict resid, then run D-F on uhat.

*predict uhat, resid
*the look at t-value. If there is a unit root in uhat, you do not have cointegration and cannto trust this regression to be a sensible regression.

*Fun with graphs. Saving for future reference and use
*graph twoway (scatter visitno age)
*graph twoway (scatter visitno age) (lfit visitno age)
*graph twoway (scatter visitno age, color(gs6) msymbol(Oh) msize(vtiny) jitter (6))
*hist age if medcare==0 , discrete percent
*hist age if medcare==1 , discrete percent
*graph twoway (scatter visitno age , color(gs6) msymbol(Oh) msize(vtiny) jitter (6)) (lfit) visitno age if age<65) (lfit visitno age if age>=65)








